package com.haiyi.activiti.engine.framework.activiti.manager.impl;

import com.haiyi.activiti.engine.model.entity.User;
import com.haiyi.activiti.engine.framework.activiti.manager.UserManager;
import com.haiyi.activiti.engine.framework.basic.DateTimeAdapter;
import com.haiyi.activiti.engine.framework.basic.IdBuilder;
import com.haiyi.activiti.engine.framework.basic.SecurityAdapter;
import com.haiyi.activiti.engine.query.UserQuery;
import com.haiyi.activiti.engine.service.IUserService;
import com.haiyi.activiti.engine.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p> </p>
 *
 * <pre> Created: 2018/12/5 16:13 </pre>
 *
 * @author hlu
 * @version 1.0
 * @since JDK 1.7
 */
@Component
@Transactional(rollbackFor = Exception.class)
public class UserManagerImpl implements UserManager {
    @Autowired
    private IUserService userService;
    @Autowired
    private IdBuilder idBuilder;
    @Autowired
    private SecurityAdapter securityAdapter;
    @Autowired
    private DateTimeAdapter dateTimeAdapter;

    @Override
    public void createUser(User user) {
        if(user.getIid()==null){
            user.setIid(idBuilder.getId());
        }
        user.setCreater(securityAdapter.getCurrentUser().getIid());
        user.setCreateTime(dateTimeAdapter.getCurrentDate());
        Assert.isTrue(userService.insert(user),"创建用户失败！");
    }

    @Override
    public void updateUser(User user) {
        Assert.isNotNull(user.getIid(),"用户id不能为空！");
        user.setUpdateTime(dateTimeAdapter.getCurrentDate());
        user.setUpdater(securityAdapter.getCurrentUser().getIid());
        Assert.isTrue(userService.updateById(user),"修改用户失败！");
    }

    @Override
    public void deleteUser(String userId) {
        Assert.isNotNull(userId,"用户id不能为空！");
        Assert.isTrue(userService.deleteById(userId),"删除用户失败！");
    }

    @Override
    public UserQuery createUserQuery() {
        final UserQuery query = userService.createQuery();
        Assert.isNotNull(query,"创建UserQuery失败！");
        return query;
    }
}
